package com.keyinong.caizhengting.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author：郑谦
 * @时间：2017-3-10 下午4:02:27
 * @类名：ForgetpwdActivity.java
 * @功能: 忘记密码
 */


public class ForgetpwdActivity extends Activity {
    /**
     * 返回
     */
    ImageButton ib_fanhui;
    /**
     * 手机号
     */
    EditText et_shoujihao2;
    /**
     * 验证码
     */
    EditText et_yanzhengma2;
    /**
     * 获取验证码
     */
    Button btn_huoqu2;
    /**
     * 密码
     */
    EditText et_mima2;
    /**
     * 确认密码
     */
    EditText et_querenmima2;
    /**
     * 完成
     */
    Button btn_xiugai;

    boolean isObtain = false;
    HttpTools httpTools = null;
    /**
     * 倒计时
     */
    private TimeCount time;
    private OnClickListener fogetpwdOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ib_fanhui:
                    Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case R.id.btn_huoqu2:
                    getFogetpwdyanzhengma();
                    break;
                case R.id.btn_xiugai:
                    xiugai();
                    break;

                default:
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//				getWindow().addFlags(
//						WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_forgetpwd);
        setview();
        setaddListener();
    }

    private void setaddListener() {
        ib_fanhui.setOnClickListener(fogetpwdOnClickListener);
        btn_huoqu2.setOnClickListener(fogetpwdOnClickListener);
        btn_xiugai.setOnClickListener(fogetpwdOnClickListener);
    }

    /**
     * 获取验证码
     */
    private void getFogetpwdyanzhengma() {
        if (NetWorkUtil.isNetwork(getApplicationContext()) == false) {
            Toast.makeText(getApplicationContext(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        String phone = et_shoujihao2.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            et_shoujihao2.setError("手机号为空");
            return;
        }
        if (!TextUtils.isDigitsOnly(phone)) {
            et_shoujihao2.setError("请输入正确的手机号");
            return;
        }
        if (phone.length() != 11) {
            et_shoujihao2.setError("手机号不符");
            return;
        }
        Log.i("zq", phone);
        if (httpTools == null) {
            httpTools = new HttpTools(getApplicationContext());
        }
        httpTools.forgetpwdyanzhengma(phone, new JsonGetYanzhengma());
    }

    /**
     * 修改密码
     */
    private void xiugai() {
        if (NetWorkUtil.isNetwork(getApplicationContext()) == false) {
            Toast.makeText(getApplicationContext(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        String phone = et_shoujihao2.getText().toString();
        String yanzhengma = et_yanzhengma2.getText().toString();
        String mima = et_mima2.getText().toString();
        String querenmima = et_querenmima2.getText().toString();
        if (isObtain == false) {
            Toast.makeText(getApplicationContext(), "请输入手机号码", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(yanzhengma)) {
            Toast.makeText(getApplicationContext(), "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (yanzhengma.length() != 6) {
            Toast.makeText(getApplicationContext(), "请输入正确的验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        if ("".equals(mima)) {
            Toast.makeText(getApplicationContext(), "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!mima.equals(querenmima)) {
            Toast.makeText(getApplicationContext(), "密码和确认密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
            et_mima2.setText("");
            et_querenmima2.setText("");
            return;
        }
        if (httpTools == null) {
            httpTools = new HttpTools(getApplicationContext());
        }
        httpTools.xiugaimima(yanzhengma, phone, mima, new Jsonxiugaimima());
    }

    private void setview() {
        ib_fanhui = (ImageButton) findViewById(R.id.ib_fanhui);
        et_shoujihao2 = (EditText) findViewById(R.id.et_shoujihao2);
        et_yanzhengma2 = (EditText) findViewById(R.id.et_yanzhengma2);
        et_mima2 = (EditText) findViewById(R.id.et_mima2);
        et_querenmima2 = (EditText) findViewById(R.id.et_querenmima2);
        btn_huoqu2 = (Button) findViewById(R.id.btn_huoqu2);
        btn_xiugai = (Button) findViewById(R.id.btn_xiugai);
        time = new TimeCount(60000, 1000);//构造CountDownTimer对象
    }

    private class JsonGetYanzhengma extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            time.start();//开始计时
            try {
                Log.i("zq", response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    isObtain = true;
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private class Jsonxiugaimima extends JsonHttpResponseHandler {

        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            try {
                boolean cade = response.getBoolean("code");
                String msg = response.getString("msg");
                Log.i("zq", response.toString());
                if (cade == true) {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(),
                            LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {//计时完毕时触发
            btn_huoqu2.setText("重新验证");
            btn_huoqu2.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            btn_huoqu2.setClickable(false);
            btn_huoqu2.setText(millisUntilFinished / 1000 + "秒");
        }
    }


}
